package com.cjh.wechat.websocket.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cjh.wechat.commom.pojo.Notify;
import com.cjh.wechat.commom.pojo.NotifyGroup;
import com.cjh.wechat.commom.pojo.User;
import com.cjh.wechat.commom.pojo.UserAndGroup;
import com.cjh.wechat.commom.type.ApplyStatus;
import com.cjh.wechat.commom.type.GroupMemberLevel;
import com.cjh.wechat.commom.type.ReadStatus;
import com.cjh.wechat.commom.util.BasicUtil;
import com.cjh.wechat.commom.vo.MineVo;
import com.cjh.wechat.websocket.service.NotifyGroupService;
import com.cjh.wechat.websocket.service.NotifyService;
import com.cjh.wechat.websocket.service.RedisService;
import com.cjh.wechat.websocket.service.UserAndGroupService;
import com.cjh.wechat.websocket.service.UserService;
import redis.clients.jedis.Jedis;

@Service
public class RedisUserAndGroupServieImpl implements UserAndGroupService
{
    @Resource
    private RedisService        redisService;
    @Resource
    private UserService         userService;
    @Resource
    private NotifyGroupService  notifyGroupService;
    @Resource
    private NotifyService       notifyService;
    private static final String GROUP_RELATIVE = "group";
    
    @Override
    public void insert(UserAndGroup record)
    {
        try (Jedis jedis = redisService.get())
        {
            jedis.zadd(GROUP_RELATIVE + record.getGid(), //
                    Double.parseDouble(record.getType()), record.getUid());
            jedis.zadd(GROUP_RELATIVE + record.getUid(), //
                    Double.parseDouble(record.getType()), record.getGid());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void updateGroupMember(String uid, String gid, String type) throws Exception
    {
        try (Jedis jedis = redisService.get())
        {
            Notify notify = new Notify();
            NotifyGroup notifyGroup = new NotifyGroup();
            
            notifyGroup.setGid(gid);
            notifyGroup.setFromUid(uid);
            notifyGroup.setIsRead(ReadStatus.UNREAD.value);
            notifyGroup.setDateTime(BasicUtil.getDateTime());
            notifyGroup.setType(ApplyStatus.SYS.value);
            if (GroupMemberLevel.ADMIN.value.equals(type))
            {
                notify.setContent("您被任命为群组" + gid + "的管理员");
                notifyGroup.setContent(uid + "被任命为管理员");
            }
            else
            {
                notify.setContent("您在群组" + gid + "的管理员身份被解除");
                notifyGroup.setContent(uid + "被解除管理员身份");
            }
            notifyService.insertSelective(notify);
            notifyGroupService.insertSelective(notifyGroup);
            jedis.zadd(GROUP_RELATIVE + gid, Double.parseDouble(type), uid);
            jedis.zadd(GROUP_RELATIVE + uid, Double.parseDouble(type), gid);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public List<User> findByGroupId(String gid)
    {
        List<User> users = new ArrayList<User>();
        try (Jedis jedis = redisService.get())
        {
            Set<String> sets = jedis.zrange(GROUP_RELATIVE + gid, 0, -1);
            Iterator<String> iterator = sets.iterator();
            while (iterator.hasNext())
            {
                String id = iterator.next();
                User user = userService.findById(id);
                users.add(user);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return users;
    }
    
    @Override
    public List<UserAndGroup> findByUid(String uid)
    {
        List<UserAndGroup> userAndGroups = new ArrayList<UserAndGroup>();
        try (Jedis jedis = redisService.get())
        {
            Set<String> sets = jedis.zrange(GROUP_RELATIVE + uid, 0, -1);
            Iterator<String> iterator = sets.iterator();
            while (iterator.hasNext())
            {
                String gid = iterator.next();
                double val = jedis.zscore(GROUP_RELATIVE + uid, gid);
                int type = (int) val;
                UserAndGroup userAndGroup = new UserAndGroup();
                userAndGroup.setUid(uid);
                userAndGroup.setGid(gid);
                userAndGroup.setType(type + "");
                userAndGroups.add(userAndGroup);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return userAndGroups;
    }
    
    @Override
    public List<MineVo> findByGid(String gid)
    {
        List<MineVo> vos = new ArrayList<MineVo>();
        try (Jedis jedis = redisService.get())
        {
            Set<String> sets = jedis.zrange(GROUP_RELATIVE + gid, 0, -1);
            Iterator<String> iterator = sets.iterator();
            while (iterator.hasNext())
            {
                String id = iterator.next();
                User user = userService.findById(id);
                MineVo vo = new MineVo();
                vo.setAvatar(user.getAvatar());
                vo.setId(user.getId());
                vo.setSign(user.getSign());
                vo.setStatus(user.getStatus());
                vo.setUsername(user.getUsername());
                vos.add(vo);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return vos;
    }
    
    @Override
    public UserAndGroup findByUidAndGid(String uid, String gid)
    {
        UserAndGroup userAndGroup = new UserAndGroup();
        try (Jedis jedis = redisService.get())
        {
            double val = jedis.zscore(GROUP_RELATIVE + uid, gid);
            int type = (int) val;
            userAndGroup.setUid(uid);
            userAndGroup.setGid(gid);
            userAndGroup.setType(type + "");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return userAndGroup;
    }
    
    @Override
    public void deleteByGid(String gid)
    {
        try (Jedis jedis = redisService.get())
        {
            Set<String> sets = jedis.zrange(GROUP_RELATIVE + gid, 0, -1);
            Iterator<String> iterator = sets.iterator();
            while (iterator.hasNext())
            {
                String id = iterator.next();
                jedis.zrem(GROUP_RELATIVE + id, gid);
            }
            jedis.zrem(GROUP_RELATIVE + gid);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void deleteByUidAndGid(String uid, String gid)
    {
        try (Jedis jedis = redisService.get())
        {
            jedis.zrem(GROUP_RELATIVE + uid, gid);
            jedis.zrem(GROUP_RELATIVE + gid, uid);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
}
